﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace csdemo
{
    public partial class Form1 : Form
    {

        //二进制
        private int refsize;
        private byte[] refbuf;
        private int matsize;
        private byte[] matbuf;

        //字符串
        private byte[] refstr;
        private byte[] matstr;


        public Form1()
        {
            InitializeComponent();

            
            refsize = 0;
            refbuf = new byte[512];
            matsize = 0;
            matbuf = new byte[256];

            
            refstr = new Byte[768]; 
            matstr = new Byte[384];
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if(fpengine.OpenDevice(0, 0, 0)==1)
            {
                if(fpengine.LinkDevice()==1)
                {
                    label1.Text = "设备打开成功";
                }
                else
                {
                    label1.Text="打开设备失败";
                }
            }
            else
            {
                label1.Text="打开设备失败";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fpengine.EnrolFpChar();
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            fpengine.GenFpChar();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int wm=fpengine.GetWorkMsg();
            int rm = fpengine.GetRetMsg();
            if (wm == 8)
            {
                int x = 1;
            }
            switch(wm)
            {
                case fpengine.FPM_DEVICE:
                    label1.Text="设备未打开";
                    break;
                case fpengine.FPM_PLACE:
                    label1.Text="请按手指";
                    break;
                case fpengine.FPM_LIFT:
                    label1.Text="请抬起手指";
                    break;
                case fpengine.FPM_CAPTURE:
                    //采集图像
                    break;
                case fpengine.FPM_ENROLL: 
                    {
                        if (rm == 1)
                        {                            
                            label1.Text = "登记指纹模版成功";
                            if (checkBox1.Checked)
                            {
                                fpengine.GetFpStrByEnl(ref refstr[0]);
                                string un = System.Text.Encoding.Default.GetString(refstr);
                                textBox1.Text = un;
                            }
                            else
                            {
                                fpengine.GetFpCharByEnl(ref refbuf[0], ref refsize);
                            }
                        }
                        else
                        {
                            label1.Text = "登记指纹模版失败";
                        }
                        timer1.Enabled = false;
                    }
                    break;
                case fpengine.FPM_GENCHAR:    
                    {
                        if(rm==1)
                        {
                            label1.Text="采集特征点成功";
                            if (checkBox1.Checked)
                            {
                                fpengine.GetFpStrByGen(ref matstr[0]);
                                string un = System.Text.Encoding.Default.GetString(matstr);
                                textBox2.Text = un;
                            }
                            else
                            {
                                fpengine.GetFpCharByGen(ref matbuf[0], ref matsize);
                            }
                            if (textBox1.TextLength > 0 || refsize > 0)
                            {
                                int scope = 0;
                                if(checkBox1.Checked)
                                {
                                    if(textBox1.TextLength > 0)
                                    {
                                        //scope = fpengine.MatchTemplateEx(ref matstr[0], ref refstr[0]);
                                        byte[] gmat = System.Text.Encoding.Default.GetBytes(textBox2.Text);
                                        byte[] gref = System.Text.Encoding.Default.GetBytes(textBox1.Text);
                                        scope = fpengine.MatchTemplateEx(ref gmat[0], ref gref[0]);   
                                    }                                    
                                }
                                else
                                {
                                    if (refsize > 0)
                                    {
                                        scope = fpengine.MatchTemplate(ref refbuf[0], ref matbuf[0]);
                                        scope = scope + fpengine.MatchTemplate(ref refbuf[256], ref matbuf[0]);
                                    }                                    
                                }                                
                                if(scope>30)
                                {
                                    label1.Text = "比对成功";  
                                }
                                else
                                {
                                    label1.Text = "比对失败";  
                                }                                
                            }
                        }
                        else
                        {
                            label1.Text = "采集特征点失败";
                        }
                        timer1.Enabled = false;
                    }                    
                    break;
                case fpengine.FPM_NEWIMAGE:		
                    //label1.Text="指纹图像";
                    {
                        System.Drawing.Bitmap bmp = new Bitmap(255, 288);
                        Graphics g = Graphics.FromImage(bmp);
                        fpengine.DrawImage(g.GetHdc(), 0, 0);
                        g.Dispose();
                        pictureBox1.Image = bmp;
                    }
                    break;
                defalut:
                    int x = 1;
                    break;
            }
        }
    }
}

